#ifndef _TIMER_H_
#define _TIMER_H_

#include <chrono>
#include <iostream>
#include <ostream>

using namespace std::chrono;

/**
* @brief Timer class with microsecond accuracy.
*/
class  Timer
{

public:

  Timer();

  ~Timer();

  /**
  * @brief Reset the timer to zero.
  */
  inline void reset() {
    start_ = high_resolution_clock::now();
  }

  static inline int now() {
    return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
  }
  
  /**
  * @brief Get the elapsed time in seconds.
  * @return Time elapsed in second
  */
  inline double elapsed() const {
    return elapsedMs() / 1000.;
  }

  /**
  * @brief Get the elapsed time in milliseconds.
  * @return Time elapsed in milliseconds
  */
  inline double elapsedMs() const {
    const auto end_ = high_resolution_clock::now();
    return std::chrono::duration_cast<std::chrono::milliseconds>(end_ - start_).count();
  }

private:

  high_resolution_clock::time_point start_;
};

/**
* print the elapsed time
* @brief out Stream in which time is written
* @param tim Timer to output
* @return stream after write operation
*/
std::ostream& operator << (std::ostream& out, const Timer& tim);

#endif
